<?php ob_start();session_start();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Quản lý bài viết</title>
<link href="../Twitter_Bootstrap/css/bootstrap.css" rel="stylesheet" type="text/css" />
<link href="../CSS/css_admin.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="../CSS/validationEngine.jquery.css"/>

<script type="text/javascript" src="../Twitter_Bootstrap/js/jquery.js"></script>
<script type="text/javascript" src="../Twitter_Bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../Scripts/jquery.validationEngine.js"></script>
<script type="text/javascript" src="../Scripts/jquery.validationEngine-vi.js"></script>
<script type="text/javascript" src="../Scripts/GroupRoleManagementJS.js"></script>
<style type="text/css">
<!--
#tbResult td,#tbResult th
{
	text-align:center;
}
-->
</style>
</head>

<body>
<div class="container" id="Container">
<!--Menu top -->
  <div id="menu">
  	<?php
		include("MainMenu.php");
		include("../Connections/connect_admin.php");
	?>
    </div>
  </div>
  <div id="divContain">
  <!--Content -->
   <div class="divTitle"><span class="titlePage" >Quản lý bài viết</span></div>
 		<div style="padding-top:30px" >
     <form action="" method="get" id="formSearch" class="form-horizontal">
 		
        <input type="text" id="txtKeyword" name="keyword" placeholder="Từ khóa...">
       <select name="topic" id="cbxTopic">
    	<option value="0">---Tất cả chuyên mục---</option>
    	 <?php	  
		function Menu($cap = 0,$gach = '&gt;&gt;  ', $arr = NULL){  
		if(!$arr) $arr = array();//khoi tao 1 array co ten la arr  
		$sql = mysql_query("SELECT * FROM topic WHERE IsDeleted = 0 and ParentID = ".$cap." order by OrderIndex");  
		while($row = mysql_fetch_assoc($sql)){  
		$arr[] = array('id'=>$row['TopicID'],'ten'=>$gach.$row['TopicName']);  
		$arr = Menu($row['TopicID'],$gach.'&gt;&gt;   ',$arr);  
		}  
		return $arr;  
		}  
		$Menu = Menu(0);  

		 foreach($Menu as $k=>$row)
		 {
		 ?>
		 <option value="<?php echo $row["id"]; ?>" <?php if(isset($_GET["topic"])&&$_GET["topic"]==$row["id"]) echo 'selected="selected"'; ?>><?php echo $row["ten"]; ?></option>
		<?php
		 }
		?>       	
        </select>
        
        <select name="type" id="cbxType" style="max-width:120px">
        	<option value="-1" <?php if(isset($_GET["type"])&& $_GET["type"]==-1) echo 'selected="selected"'; ?>>---Loại tin---</option>
            <option value="0"  <?php if(isset($_GET["type"])&& $_GET["type"]==0) echo 'selected="selected"'; ?>>Bình thường</option>
            <option value="1"  <?php if(isset($_GET["type"])&& $_GET["type"]==1) echo 'selected="selected"'; ?>>Hình ảnh</option>
            <option value="2"  <?php if(isset($_GET["type"])&& $_GET["type"]==2) echo 'selected="selected"'; ?>>Video</option>
        </select>
        
        <select name="review" id="cbxReview" style="width:180px">
        	<option value="-1"  <?php if(isset($_GET["review"])&& $_GET["review"]==-1) echo 'selected="selected"'; ?>>---Tình trạng duyệt---</option>
            <option value="0" <?php if(isset($_GET["review"])&& $_GET["review"]==0) echo 'selected="selected"'; ?>>Chưa duyệt</option>
            <option value="1" <?php if(isset($_GET["review"])&& $_GET["review"]==1) echo 'selected="selected"'; ?>>Đã duyệt</option>
        </select>
        <button class="btn" type="submit" id="btnSearch"><i class=" icon-search"></i> Tìm kiếm</button>
    </form>
    
     <?php
	if(isset($_GET["e"]))
	{
		$error=intval($_GET["e"]);
		switch($error)
		{
			case 0:
	?>
    <div class="alert alert-success">
    	    <a class="close" data-dismiss="alert" href="#">&times;</a>
            <strong>Thêm mới bài viết THÀNH CÔNG</strong>
    </div>
    <?php
			break;
			case 1:
	?>
     <div class="alert alert-error">
    	    <a class="close" data-dismiss="alert" href="#">&times;</a>
            <strong>Thêm mới bài viêt THẤT BẠI</strong>
    </div>
     <?php
	 		break;
			case 2:
	?>
     <div class="alert alert-success">
    	    <a class="close" data-dismiss="alert" href="#">&times;</a>
            <strong>Cập nhật bài viết THÀNH CÔNG</strong>
    </div>
     <?php
	 		break;
			case 3:
	?>
     <div class="alert alert-error">
    	    <a class="close" data-dismiss="alert" href="#">&times;</a>
            <strong>Cập nhật bài viết THẤT BẠI</strong>
    </div>
    <?php
			break;
		}	
		
	}
	?>
    
    </div>
    <table class="table table-bordered table-striped  " id="tbResult" width="100%" border="0" cellspacing="0" cellpadding="0" >
  <tr>
  	<th scope="col" class="hide">Mã</th>
  	<th scope="col">Hình ảnh</th>
    <th scope="col">Tên bài</th>
    <th scope="col">Chuyên mục</th>
    <th scope="col">Người viết</th>
    <th scope="col">Ngày tạo</th>
    <th scope="col">Loại tin</th>
    <th scope="col">Đã duyệt</th>
    <th scope="col"></th>
    <th scope="col"></th>
  </tr>
  <?php
  	$url='../Images/Img_News/';
  	$page=1;
	$recordPerPage=10;
	$keyword='';
	if(isset($_GET["page"]))
		{
			$page=intval($_GET["page"]);
		}
	$index=($page-1)*$recordPerPage;
  if(isset($_GET["keyword"]))
  {
	$keyword=trim($_GET["keyword"]);
	$topic=intval($_GET["topic"]);
	$type=intval($_GET["type"]);
	$review=intval($_GET["review"]);
	//$topicList=Menu($topic);
	
	$query="SELECT n.NewsID,n.NewsType, n.Title, n.CreatedUser, n.CreatedDate, n.IsReview,t.TopicName,n.Image FROM news n INNER JOIN topic t ON t.TopicID = n.TopicID WHERE n.IsDeleted=0 and (n.Title like '%$keyword%' OR '$keyword' ='') and (n.TopicID = $topic or $topic=0) and ($review =-1 or n.IsReview =$review) and ($type=-1 or n.NewsType=$type) order by n.CreatedDate desc";
  }
  else
  {
	 $query="SELECT n.NewsID,n.NewsType, n.Title, n.CreatedUser, n.CreatedDate, n.IsReview,n.Image,t.TopicName FROM news n INNER JOIN topic t ON t.TopicID = n.TopicID WHERE n.IsDeleted=0 order by n.CreatedDate  desc";
  }
  $sql=$query." Limit $index,$recordPerPage";
  $result=mysql_query($sql);
  while($row=mysql_fetch_assoc($result))
  {
  ?>
  <tr>
  	<td class="hide"><?php echo $row["NewsID"]; ?></td>
    <td><img src="<?php echo $url.$row["Image"]; ?>" width="100px" alt="" /></td>
    <td><?php echo $row["Title"]; ?></td>
    <td><?php echo $row["TopicName"]; ?></td>
    <td><?php echo $row["CreatedUser"]; ?></td>
    <td><?php $sqldate=strtotime($row["CreatedDate"]); $date=date('d/m/y',$sqldate); echo $date; ?></td>
    <td><?php if($row["NewsType"]==0) { echo 'Bình thường';}else if($row["NewsType"]==1){ echo 'Hình ảnh';} else { echo 'Video';} ?></td>  
    <td><input type="checkbox" disabled="disabled" <?php if($row["IsReview"]==1) echo 'checked="checked"'; ?> /></td>
    <td><a href="#" class="delete" title="Xóa"><img src="../Images/Icons/delete.png" alt="delete"  /></a></td>
    <td><a href="WriteNews.php?id=<?php echo $row["NewsID"]; ?>" title="Sửa"><img src="../Images/Icons/page_edit.png" alt="update" /></a></td>
   
  </tr>
  <?php }?>
</table>
<script type="text/javascript">
	$(document).ready(function(){
			$('.delete').live('click',function(){
						if(confirm('Bạn thật sự muốn xóa?'))
						{
							var $temp=$(this);
							var id=$(this).parent().parent().children().eq(0).text();
							$.ajax({ 
								   	url:"process/News_process.php",
									data:{task:"delete",user:'<?php echo $_SESSION["username"]; ?>', newsid:id},
									type:"post",
									dataType:"json",
									success:function(res)
												{
													if(res.ok)
													{
													$temp.parent().parent().remove();
													}
												 alert(res.msg);
																			 
												}
								});
						}
			});
							   
	});
</script>
	    <div class="pagination pull-right">
      <ul>
        <?php require("process/funcPaging.php");
			
			if(isset($_GET["keyword"])&& $keyword!='')
			{
				$keyword=$_GET["keyword"];
				$topic=intval($_GET["topic"]);
				$type=$_GET["type"];
				$review=$_GET["review"];
				$link="News.php?keyword=".$keyword."&topic=".$topic."&type=".$type."&review=".$review."&";
			}
			else
			{	
				$link="News.php?";
			}
			$resAll=mysql_query($query);
			$total=mysql_num_rows($resAll);
			
			Paging($total,$page,5,$recordPerPage,$link);
 		?>
        </ul>
    </div>
	</div>
  <!--footer -->
  <div id="footer">
     <?php
     	include("Footer.php");
	 ?>
  </div>
  </div>
  

</body>
</html>
